import { Controller, Post, Body, Get, Query, Delete } from '@nestjs/common';
import { RoleService } from './role.service';
import { SaveRoleDto } from './dto/role.dto';
import {
  PageQueryParam,
  PaginationPipe,
} from '../../../common/pipes/pagination.pipe';
import { RequirePermission } from '../../../common/decorators/require-permission.decorator';
import { Like } from 'typeorm';

@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @RequirePermission('sys:role:save')
  @Post('save')
  async save(@Body() body: SaveRoleDto) {
    return await this.roleService.save(body);
  }

  @RequirePermission('sys:role:find')
  @Get('find-page')
  async findPage(
    @Query(PaginationPipe) param: PageQueryParam,
    @Query('name') name?: string,
  ) {
    const { rows, total } = await this.roleService.findAndCount({
      where: { name: name ? Like(`%${name}%`) : undefined },
      relations: {
        menus: true,
        permissions: true,
      },
      ...param,
    });
    return {
      rows,
      total,
    };
  }

  @RequirePermission('sys:role:delete')
  @Delete()
  async remove(@Body('ids') ids: string[]) {
    return await this.roleService.softDelete(ids);
  }
}
